package org.example;

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import org.keyvalue.KeyValueServiceOuterClass.*;
import org.keyvalue.KeyValueServiceGrpc;

public class KeyValueClient {
    public static void main(String[] args) throws InterruptedException {
        ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 50051)
                .usePlaintext()
                .build();

        KeyValueServiceGrpc.KeyValueServiceBlockingStub stub = KeyValueServiceGrpc.newBlockingStub(channel);

        // 使用 stub 调用 gRPC 服务的方法
        PutRequest putRequest = PutRequest.newBuilder().setKey("myKey").setValue("myValue").build();
        PutResponse putResponse = stub.put(putRequest);
        System.out.println("Put Response: " + putResponse.getSuccess());

        for(int i=1;i<10;i++) {
            Thread.sleep(1000);
            String s="myKey"+i;
            GetRequest getRequest = GetRequest.newBuilder().setKey(s).build();
            GetResponse getResponse = stub.get(getRequest);
            System.out.println("Get Response: " + getResponse.getValue());
        }

        channel.shutdown();
    }
}
